<template>
  <u-swiper keyName="image" :list="bannerList" indicator height="170" img-mode="scaleToFill" @click="swiperClick"></u-swiper>
  <view class="header" @click="goPage('/pages/home/searchPage')">
    <u-input readonly placeholder="搜索工厂和动态" border="none" shape="circle" prefixIcon="search" prefixIconStyle="font-size: 22px;color: #A8A8A8"></u-input>
  </view>
  <view class="main">
    <template v-if="factoryList?.length > 0">
      <CompanyCard @boxClick="boxClick(item)" v-for="(item, index) in factoryList" :key="index" :info="item" :isBtn="false" />
      <u-loadmore height="30" :status="status" />
    </template>
    <view v-else style="width: 100%; display: flex; justify-content: center">
      <u-empty mode="list"></u-empty>
    </view>
  </view>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import CompanyCard from "../../components/companyCard.vue";
import { getByType } from "@/api/my";
import { goPage } from "@/utils/commonFn";
//@ts-ignore
import { onReachBottom, onShow } from "@dcloudio/uni-app";
import { industryFactoryList, recommendFactory } from "@/api/home";
const { id, hotFlag } = defineProps<{
  id: string;
  hotFlag: string;
}>();
const bannerList = ref();
onMounted(async () => {
  const res = await getByType({ type: "2" });
  bannerList.value = res.data;
});
const formData = ref({
  pageNum: 1,
  pageSize: 10,
  industryId: id || "",
  hotFlag: hotFlag || "",
});
onMounted(() => {
  rest();
});
const status = ref("loadmore");
const factoryList = ref<HomeTypes.factoryList[]>([]);
const getList = async () => {
  status.value = "loadmore";
  let res;
  if (id) {
    res = await industryFactoryList(formData.value);
  } else {
    res = await recommendFactory(formData.value);
  }
  factoryList.value = factoryList.value.concat(res.rows);
  if (factoryList.value.length >= (res.total as number)) {
    status.value = "nomore";
  } else {
    status.value = "loadmore";
  }
};
const rest = () => {
  formData.value.pageNum = 1;
  factoryList.value = [];
  status.value = "loadmore";
  getList();
};
onReachBottom(() => {
  formData.value.pageNum++;
  getList();
});
const boxClick = (info: HomeTypes.factoryList) => {
  if (uni.getStorageSync("userInfo").level !== "1") {
    uni.showModal({
      title: "提示",
      cancelText: "取消",
      confirmText: "开通会员",
      content: "您未开通会员，暂时无法查看",
      success: function (res) {
        if (res.confirm) {
          goPage("/pages/myPage/activateMembership");
        }
      },
    });
    return;
  }
  goPage(`/pages/findJob/factoryDetails?id=${info.id}`);
};
const swiperClick = (e: number) => {
  goPage(`/pages/message/newsDetails?id=${bannerList.value[e].id}`);
};
</script>

<style lang="less" scoped>
.header {
  padding: 10rpx 24rpx;
  background: #fff;
  /deep/ .u-input {
    background: #f7f8fa;
    padding: 12rpx !important;
    margin-right: 10rpx;
  }
}
</style>
